﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Web.Security;
using System.Security.Cryptography.X509Certificates;

//using wse = Microsoft.Web.Services.Security.X509;
using bcx509 = Org.BouncyCastle.X509;


namespace iPdf
{
    class SystemCertificateStore
    {
        public static X509CertificateCollection SystemCertificates
        {
            get
            {
                X509CertificateCollection sysCerts = new X509CertificateCollection();

                //wse.X509CertificateStore store = new wse.X509CertificateStore(wse.X509CertificateStore.StoreProvider.System, wse.X509CertificateStore.StoreLocation.LocalMachine, wse.X509CertificateStore.RootStore);
                //store.Open();
                //sysCerts = store.Certificates;

                return sysCerts;
            }
        }

        public static bool Contains(string certName)
        {
            X509CertificateCollection sysCerts = SystemCertificates;

            foreach(X509Certificate cert in sysCerts)
            {
                if(cert.Subject == certName)
                {
                    return true;
                }
            }

            return false;
        }

        public static bcx509.X509Certificate GetCertificate(string certName)
        {
            X509CertificateCollection sysCerts = SystemCertificates;

            foreach (X509Certificate cert in sysCerts)
            {
                if (cert.Subject == certName)
                {
                    bcx509.X509CertificateParser certParser = 
                        new Org.BouncyCastle.X509.X509CertificateParser();
                    return certParser.ReadCertificate(cert.GetRawCertData());
                }
            }

            return null;
        }
    }
}
